//
//  NSSetHOMAdditions.m
//  HigherOrderMessaging
//
//  Created by Ofri Wolfus on 24/02/06.
//  Copyright 2006 Ofri Wolfus. All rights reserved.
//

#import "NSSetHOMAdditions.h"
#import "NSArrayHOMAdditions.h"
#import "HOMCArrayIterations.h"
#import "HOMUtilities.h"


@implementation NSSet (NewHOMIteration)

- (id)collect:(Message *)argumentMessage {
	unsigned count = [self count];
	DPAssert(argumentMessage != nil, @"The passed message must not be nil");
	
	if (count > 0U) {
		id	*objs = malloc(sizeof(id) * count);
		
		CFSetGetValues((CFSetRef)self, (const void **)objs);
		
		return hom_collectv(objs, count, YES, [NSMutableSet class], argumentMessage);
	}
	
	return nil;
}

- (id)selectWhere:(Message *)firstMessage, ... {
	unsigned count = [self count];
	DPAssert(firstMessage != nil, @"The passed message must not be nil");
	
	if (count > 0U) {
		id		*objs = malloc(sizeof(id) * count);
		va_list	va;
		
		CFSetGetValues((CFSetRef)self, (const void **)objs);
		va_start(va, firstMessage);
		
		return hom_selectWhere(objs, count, YES, [NSMutableSet class], firstMessage, va);
	}
	
	return nil;
}

- (id)rejectWhere:(Message *)firstMessage, ... {
	unsigned count = [self count];
	DPAssert(firstMessage != nil, @"The passed message must not be nil");
	
	if (count > 0U) {
		id		*objs = malloc(sizeof(id) * count);
		va_list	va;
		
		CFSetGetValues((CFSetRef)self, (const void **)objs);
		va_start(va, firstMessage);
		
		return hom_rejectWhere(objs, count, YES, [NSMutableSet class], firstMessage, va);
	}
	
	return nil;
}

- (id)selectSingleWhere:(Message *)firstMessage, ... {
	unsigned count = [self count];
	DPAssert(firstMessage != nil, @"The passed message must not be nil");
	
	if (count > 0U) {
		id		*objs = malloc(sizeof(id) * count);
		va_list	va;
		
		CFSetGetValues((CFSetRef)self, (const void **)objs);
		va_start(va, firstMessage);
		
		return hom_selectSingleWhere(objs, count, YES, firstMessage, va);
	}
	
	return nil;
}

- (id)rejectSingleWhere:(Message *)firstMessage, ... {
	unsigned count = [self count];
	DPAssert(firstMessage != nil, @"The passed message must not be nil");
	
	if (count > 0U) {
		id				*objs = malloc(sizeof(id) * count);
		NSMutableSet	*result = [[self mutableCopy] autorelease];
		va_list	va;
		
		CFSetGetValues((CFSetRef)self, (const void **)objs);
		va_start(va, firstMessage);
		
		[result removeObject:hom_selectSingleWhere(objs, count, YES, firstMessage, va)];
		
		return result;
	}
	
	return nil;
}

- (id)each {
	return [[self allObjects] each];
}

@end
		
